package com.dm.cloud.core.exception;

import com.dm.cloud.core.mode.ResultModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
@Slf4j
@Order(Integer.MAX_VALUE)
public class ErrorAdvice {

    @ExceptionHandler(CustomException.class)
    public ResponseEntity<ResultModel> handleCustomException(CustomException e) {
        log.error("输出自定义通用异常:"+e.getMsg(),e);
        return ResponseEntity.status(e.getCode())
                .body(ResultModel.err(e.getCode(),e.getMsg()));
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<ResultModel> handleException(Exception e) {
        log.error(e.getMessage(),e);
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                .body(ResultModel.serverErr("操作失败，请联系管理员"));
    }

}
